#!/usr/bin/env python

# ########################################
# TESTS
# ########################################

import sys

sys.path.append('..')

import unittest
import datetime

import glassvine.persistance as persistance

from glassvine.properties import props


class test_database_helper(unittest.TestCase):

    def setUp(self):
        myProp = props()
        self.config = myProp.load_properties('cache.conf')
        self.config['database_file'] = ':memory:'
        self.helper = persistance.load_plugin('glassvine.plugins.sqlite',
            self.config)

    def test_db_information(self):
        '''This will test and display information concerning the database
        itself (number of allowed pages, max size on disk etc...)'''
        c = self.helper.conn.cursor()
        c.execute('''begin''')
        c.execute('''pragma page_size''')
        page_size = list(c).pop()[0]
        c.execute('''pragma max_page_count''')
        max_page_count = list(c).pop()[0]
        c.execute('''pragma encoding''')
        encoding = list(c).pop()[0]
        c.execute('''commit''')

        max_disk_space = page_size * max_page_count
        max_disk_space_mb = max_disk_space / 1024 / 1024

        #print "---"
        #print "Database information:"
        #print "Page size\t\t",page_size,"bytes"
        #print "Max page count:\t\t",max_page_count
        #print "Maximum disk space:\t",max_disk_space_mb,"Mbytes"
        #print "Encoding:\t\t", encoding

        self.assert_(encoding == 'UTF-8')
        self.assertEqual(max_disk_space_mb, long(self.helper.max_db_size))


    def test_insertion(self):
        html = '''<html><head><title>Test</title></head><body><h1>Hello World!</h1></body></html>'''
        url = 'http://www.google.com/'
        mime_type = 'text/html'
        date = datetime.datetime.now()

        self.helper.insert_cache(url, mime_type, date, html)

        result = self.helper.retrieve_entry(url)

        self.assert_(result)
        self.assertEqual(result['url'], url)
        self.assertEqual(result['type'], mime_type)
        self.assertEqual(result['date_fetched'], date)
        self.assertEqual(result['content'], html)

    def test_binary_insertion(self):
        with open('../tests/logo.png', 'rb') as logo_file:
            content = logo_file.read()
        url = 'http://www.google.com/logo.png'
        mime_type = 'image/png'
        date = datetime.datetime.now()


        self.helper.insert_cache(url, mime_type, date, content)

        result = self.helper.retrieve_entry(url)

        self.assert_(result)
        self.assertEqual(result['url'], url)
        self.assertEqual(result['type'], mime_type)
        self.assertEqual(result['date_fetched'], date)
        self.assertEqual(result['content'], content)

    def test_date_conversion(self):

        string_date = "2009-08-03 14:30:34.123"
        int_year = 2009
        int_month = 8
        int_day = 3
        int_hour = 14
        int_minute = 30
        int_second = 34
        int_milisec = 123

        #before = datetime.datetime.now()
        obj_return = self.helper._datetime_from_iso_format(string_date)
        #after = datetime.datetime.now()

        obj_date = datetime.datetime(int_year, int_month, int_day, int_hour, int_minute, int_second, int_milisec)
        #print "---"
        #print "Conversion from String to Datetime took:", after - before
        self.assertEqual(obj_date, obj_return)

    def test_get_insert_date_for_url(self):
        url = "http://localhost:8080"
        mime_type = "text/html"
        date = datetime.datetime.now()
        html = ""
        self.helper.insert_cache(url, mime_type, date, html)
        
        date_returned = self.helper.get_insert_date_for_url(url)
        
        self.assertEqual(date, date_returned)
        
        void_date = self.helper.get_insert_date_for_url("I do not exist")
        
        self.assertEqual(void_date, None)
        
    def test_insert_headers(self):
        url = "http://localhost:8080"
        mime_type = "text/html"
        date = datetime.datetime.now()
        html = ""
        
        headers = {}
        headers['test-header1'] = "Test"
        headers['test-header2'] = "Test"
        headers['test-header3'] = "Test"
        
        self.helper.insert_cache(url, mime_type, date, html, headers)
        
        result = self.helper.retrieve_entry(url)
        
        self.assert_(result)
        self.assert_(result['headers'])
        self.assertEquals(result['headers']['test-header1'], "Test")
        self.assertEquals(result['headers']['test-header2'], "Test")
        self.assertEquals(result['headers']['test-header3'], "Test")
        
          


if __name__ == "__main__" :
    unittest.main()
